
angular.module('LogisticsDelivery')

.service('LogisticsDeliveryService', ['$q', '$filter', 'Http', 'User', 'DELIVERYCONSTANTS', 
    function ($q, $filter, Http, User, DELIVERYCONSTANTS) {
        var o = {},
            _deliveryRecords;

        o.getWareHouses = function () {
            return Http.post({
                url: DELIVERYCONSTANTS.getWareHousesURL
            });
        };

        o.getDeliveryRecords = function (wareHouse, status, forceUpdate) {
            var defer = $q.defer();

            forceUpdate = !!forceUpdate;
            $q.when(forceUpdate ? queryDeliveryRecords(wareHouse) : 
                (_deliveryRecords ? _deliveryRecords : queryDeliveryRecords(wareHouse))
            ).then(function (deliveryRecords) {
                var records;
                if (status !== -1) {
                    records = angular.copy($filter('filter')(deliveryRecords, { Status: status }));
                } else {
                    records = angular.copy(deliveryRecords);
                }
                formatDeliveryRecords(records);
                defer.resolve(records);
            });

            return defer.promise;
        };
        o.updateDeliveryStatus = function (deliveryIds) {
            return Http.post({
                url: DELIVERYCONSTANTS.updateDeliveryStatusURL,
                data: {
                    DeliveryIds: deliveryIds,
                    UserId: User.get('_id')
                }
            });
        };

        return o;

        function queryDeliveryRecords (wareHouse) {
            var defer = $q.defer();

            Http.post({
                url: DELIVERYCONSTANTS.getDeliveryRecordsURL,
                data: {
                    WareHouse: wareHouse
                }
            }).then(function (records) {
                _deliveryRecords = records;
                defer.resolve(angular.copy(_deliveryRecords));
            });

            return defer.promise;
        }

        function formatDeliveryRecords (records) {
            angular.forEach(records, function (record) {
                if (record.Container) {
                    record.Container = record.Container.Code;
                }
                if (record.StockPerson) {
                    record.StockPerson = record.StockPerson.Name;
                }
                if (record.DemandDepartment) {
                    record.DemandDepartment = record.DemandDepartment.Name;
                }
                if (record.ReadyTime) {
                    record.ReadyTime = $filter('date')(record.ReadyTime, 'yyyy-MM-dd hh:mm:ss');
                }
                if (record.Status === 0) {
                    record.StatusName = '待配送';
                } else if (record.Status === 1) {
                    record.StatusName = '配送中';
                } else if (record.Status === 2) {
                    record.StatusName = '已配送';
                }
                record.checked = false;
            });
        }
    }
]);